package com.rp.common.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rp.system.user.entity.SysUser;
import com.rp.system.user.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * 类说明: 根据当前用户的部门orgcode查询
 *
 * @author ****
 * @date 2020-12-04 16:48  新建
 */
@Service
@Slf4j
public class Selectway {

    @Autowired
    private SysUserService sysUserService;


    @Value("${spring.datasource.druid.driverClassName}")
    private String driverClassName;
    @Value("${spring.datasource.druid.url}")
    private String dbUrl;
    @Value("${spring.datasource.druid.username}")
    private String username;
    @Value("${spring.datasource.druid.password}")
    private String password;






    public SysUser getSysUser() {
        String username = getUserName();
        if(NullIs.isEmpty(username)){
            return null;
        }
        return sysUserService.findSysUserByUsername(getUserName());
    }

    /**
     * @return java.lang.String
     * @Author HuJingYu
     * @Date 17:00 2020-12-22
     * @Description 登录用户的用户名
     */
    public String getUserName() {
        return (String) SecurityUtils.getSubject().getPrincipal();
    }

    /**
     * @param queryWrapper QueryWrapper
     * @param string       模糊查询字符串
     * @param tableName    表名
     * @param alias        表别名
     * @param fields       字段名
     * @return void
     * @Author HuJingYu
     * @Date 16:56 2020-12-22
     * @Description
     */
    public <T> void fields(QueryWrapper<T> queryWrapper, String string, String tableName, String alias, String[] fields) {
        if (!NullIs.isEmpty(string)) {
            String result = "";
            if (!NullIs.isEmpty(tableName)) {
                result = result + getFields(tableName, alias);
            }
            if (!NullIs.isEmpty(fields)) {
                if (!NullIs.isEmpty(result)) {
                    result = result + ",";
                }
                result = result + getString(fields);
            }
            result = "CONCAT_WS(" + result + ")";
            queryWrapper.like(result, string);
        }
        //字典查询
//        queryWrapper.getClass().getFields();
//        System.out.println(queryWrapper.getEntity());
//        for (Field field : DictAspect.getAllFields(queryWrapper.getEntity())) {
//            if (field.getAnnotation(Dict.class) != null) {
//                String code = field.getAnnotation(Dict.class).dicCode();
//                //翻译字典值对应的txt
//                String textValue = translateDictValue(code, string);
//                queryWrapper.or().like(code, textValue);
//            }
//        }
    }

    /**
     * @param tableName 表名
     * @param alias     表的别名
     * @return java.lang.String
     * @Author HuJingYu
     * @Date 15:37 2020-12-10
     * @Description 根据表名获取 模糊查询的全部字段 注:数据量多时不适合
     */
    public String getFields(String tableName, String alias) {
        try {
            Connection conn = null;
            Class.forName(driverClassName);
            conn = DriverManager.getConnection(dbUrl, username, password);
            String sql = "select * from " + tableName;
            PreparedStatement stmt;
            stmt = conn.prepareStatement(sql);
            StringBuffer stringBuffer = new StringBuffer();
            ResultSet rs2 = stmt.executeQuery("show full columns from " + tableName);
            if (NullIs.isEmpty(alias)) {
                while (rs2.next()) {
                    stringBuffer.append(rs2.getString("Field") + ",");
                }
            } else {
                while (rs2.next()) {
                    stringBuffer.append(alias + "." + rs2.getString("Field") + ",");
                }
            }
            return stringBuffer.toString().substring(0, stringBuffer.toString().length() - 1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param a 数据库字段数组
     * @return java.lang.String
     * @Author HuJingYu
     * @Date 16:52 2020-12-22
     * @Description
     */
    public String getString(String[] a) {
        if (a == null) {
            return "";
        }

        int iMax = a.length - 1;
        if (iMax == -1) {
            return "";
        }
        StringBuilder b = new StringBuilder();
        for (int i = 0; ; i++) {
            b.append(String.valueOf(a[i]));
            if (i == iMax) {
                return b.toString();
            }
            b.append(", ");
        }
    }

}
